//所有的班级数据
const { list } = require('./data.js');
//解析post数据
function paresPostData(ctx) {
	return new Promise((resolve, reject) => {
		try {
			let postData = ''
			ctx.req.addListener('data', (data) => {
				postData += data
			})
			ctx.req.on('end', () => {
				if (ctx.request.header['content-type'] === 'application/json') {
					resolve(JSON.parse(postData))
				} else {
					resolve(parseData(postData))
				}
			})

		} catch (err) {
			reject(err)
		}
	})
}
//将键值类型的字符串转化为对象
function parseData(queryStr) {
	let queryData = {}
	let queryList = queryStr.split('&')
	for (let [index, queryItem] of queryList.entries()) {
		let itemList = queryItem.split('=')
		queryData[itemList[0]] = decodeURIComponent(itemList[1])
	}
	return queryData
}

//标准的数据返回
function rsp(data, msg = '操作成功', code = 10000) {
	return { code, msg, data }
}


module.exports = {
	list,
	rsp,
	parseData,
	paresPostData
}